package com.sugon.modules.kpi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sugon.dao.DevMaintenanceTicketDao;
import com.sugon.modules.analyse.consts.KpiTarget;
import com.sugon.modules.analyse.model.chart.KpiTargetBarEchartOptionDataVO;
import com.sugon.modules.analyse.model.vo.DeviceKpiStatParamVO;
import com.sugon.modules.kpi.model.dto.DevManintenanceTicketMonthlyStatData;
import com.sugon.modules.kpi.service.IKpiTargetComputor;
import com.sugon.modules.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： YuXD
 * @description： PM延期率KPI计算器实现类
 * @date ： 2020-11-15 下午 3:27
 * @version: 1.0
 */
@Service
public class PMYQLKPIComputorImpl implements IKpiTargetComputor {

    @Autowired
    private DevMaintenanceTicketDao devMaintenanceTicketDao;

    @Override
    public KpiTargetBarEchartOptionDataVO compute(DeviceKpiStatParamVO kpiStatParam) {
        KpiTargetBarEchartOptionDataVO kpiTargetBarEchartOption = new KpiTargetBarEchartOptionDataVO();
        // 获取设备列表
        String batchDeviceIdSql = kpiStatParam.getBatchDeviceIdSql();
        Map<Integer, Integer> monthlyTotalStatMap = getMonthlyPMTotalStat(kpiStatParam.getYearly(), batchDeviceIdSql);

        // 获取年度每月已完成工单总数
        Map<Integer, Integer> monthlyCompletedStatMap = getMonthlyPMCompletedStat(kpiStatParam.getYearly(), batchDeviceIdSql);
        /*
            从一月份到当前月份，采用已完成工单除以总工单数
            当前月份到十二月，则补0
         */
        // 当前月份
        Integer currentMonth = com.sugon.modules.utils.DateUtil.month(null);
        // 待统计的年份
        Integer statYearly = kpiStatParam.getYearly();
        // 今年年份
        int thisYear = DateUtil.thisYear();
        for (int i = 1; i <= 12; i++) {
            // 年份没到或者月份没到的一律默认为0
            if ((statYearly > thisYear) || (statYearly == thisYear && i > currentMonth)) {
                kpiTargetBarEchartOption.add(0.0);
            } else {
                int monthlyTotalNum = MapUtil.getInt(monthlyTotalStatMap, i, 0);
                if (monthlyTotalNum == 0) {
                    kpiTargetBarEchartOption.add(0.0);
                } else {
                    int monthlyCompletedNum = MapUtil.getInt(monthlyCompletedStatMap, i, 0);
                    kpiTargetBarEchartOption.add(NumberUtil.div(monthlyTotalNum - monthlyCompletedNum, monthlyTotalNum, 4, RoundingMode.HALF_UP) * 100);
                }
            }
        }
        return kpiTargetBarEchartOption;
    }

    private Map<Integer, Integer> getMonthlyPMTotalStat(Integer yearly, String batchDeviceIdSql) {
        List<DevManintenanceTicketMonthlyStatData> monthlyTotalStat = devMaintenanceTicketDao.statMonthlyTotalTimes(yearly, batchDeviceIdSql);
        if (CollectionUtil.isNotEmpty(monthlyTotalStat)) {
            return monthlyTotalStat.stream().collect(Collectors.toMap(DevManintenanceTicketMonthlyStatData::getMonth, DevManintenanceTicketMonthlyStatData::getTimes));
        } else {
            return new HashMap<>();
        }
    }

    private Map<Integer, Integer> getMonthlyPMCompletedStat(Integer yearly, String batchDeviceIdSql) {
        List<DevManintenanceTicketMonthlyStatData> monthlyTotalStat = devMaintenanceTicketDao.statMonthlyCompletedTotalTimes(yearly, batchDeviceIdSql);
        if (CollectionUtil.isNotEmpty(monthlyTotalStat)) {
            return monthlyTotalStat.stream().collect(Collectors.toMap(DevManintenanceTicketMonthlyStatData::getMonth, DevManintenanceTicketMonthlyStatData::getTimes));
        } else {
            return new HashMap<>();
        }
    }

    @Override
    public KpiTarget getType() {
        return KpiTarget.PMYQL;
    }

}
